// main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Window {
    width: 800
    height: 1200
    visible: true
    title: "每日早报 - Qt for 鸿蒙"

    Rectangle {
        anchors.fill: parent
        gradient: Gradient {
            GradientStop { position: 0.0; color: "#e3f2fd" }
            GradientStop { position: 1.0; color: "#bbdefb" }
        }

        // 标题栏
        Rectangle {
            id: header
            width: parent.width
            height: 100
            color: "#1976d2"
            
            Column {
                anchors.centerIn: parent
                spacing: 5
                
                Text {
                    text: "📰 每日早报"
                    font.pixelSize: 40
                    font.bold: true
                    color: "white"
                    anchors.horizontalCenter: parent.horizontalCenter
                }
                
                Text {
                    text: newsManager.date ? newsManager.date : "加载中..."
                    font.pixelSize: 24
                    color: "#e3f2fd"
                    anchors.horizontalCenter: parent.horizontalCenter
                }
            }
        }

        // 平台信息显示区域
        Rectangle {
            id: platformInfoBox
            width: parent.width - 40
            height: platformInfoColumn.height + 20
            anchors.top: header.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.topMargin: 10
            color: "#ffffff"
            radius: 10
            border.color: "#64b5f6"
            border.width: 2
            opacity: 0.95

            Column {
                id: platformInfoColumn
                anchors.centerIn: parent
                spacing: 8
                width: parent.width - 20

                Text {
                    text: "🖥️ 平台信息"
                    font.pixelSize: 22
                    font.bold: true
                    color: "#1976d2"
                    anchors.horizontalCenter: parent.horizontalCenter
                }

                Rectangle {
                    width: parent.width
                    height: 1
                    color: "#e3f2fd"
                    anchors.horizontalCenter: parent.horizontalCenter
                }

                Row {
                    anchors.horizontalCenter: parent.horizontalCenter
                    spacing: 15

                    Column {
                        spacing: 5

                        Text {
                            text: "平台:"
                            font.pixelSize: 18
                            color: "#546e7a"
                        }

                        Text {
                            text: "Qt版本:"
                            font.pixelSize: 18
                            color: "#546e7a"
                        }

                        Text {
                            text: "架构:"
                            font.pixelSize: 18
                            color: "#546e7a"
                        }
                    }

                    Column {
                        spacing: 5

                        Text {
                            text: platformInfo.platformName
                            font.pixelSize: 18
                            font.bold: true
                            color: "#212121"
                        }

                        Text {
                            text: platformInfo.qtVersion
                            font.pixelSize: 18
                            font.bold: true
                            color: "#212121"
                        }

                        Text {
                            text: platformInfo.cpuArchitecture
                            font.pixelSize: 18
                            font.bold: true
                            color: "#212121"
                        }
                    }
                }

                // 详细信息（可选，点击展开）
                Text {
                    visible: false
                    id: detailText
                    text: "内核: " + platformInfo.kernelVersion + "\n" +
                          "产品: " + platformInfo.prettyProductName + "\n" +
                          "ABI: " + platformInfo.buildAbi
                    font.pixelSize: 14
                    color: "#757575"
                    anchors.horizontalCenter: parent.horizontalCenter
                    wrapMode: Text.Wrap
                    width: parent.width
                }
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    detailText.visible = !detailText.visible
                }
            }
        }

        // 刷新按钮
        Button {
            id: refreshButton
            text: newsManager.loading ? "加载中..." : "🔄 刷新"
            anchors.top: platformInfoBox.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.topMargin: 15
            width: 180
            height: 60
            enabled: !newsManager.loading
            
            background: Rectangle {
                color: refreshButton.pressed ? "#1565c0" : "#1976d2"
                radius: 10
                border.color: "#0d47a1"
                border.width: 2
            }
            
            contentItem: Text {
                text: refreshButton.text
                font.pixelSize: 28
                font.bold: true
                color: "white"
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }
            
            onClicked: {
                newsManager.fetchNews()
            }
        }

        // 错误信息
        Text {
            id: errorText
            visible: newsManager.errorMessage !== ""
            text: newsManager.errorMessage
            color: "#c62828"
            font.pixelSize: 24
            anchors.top: refreshButton.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.topMargin: 10
            width: parent.width - 40
            wrapMode: Text.WordWrap
            horizontalAlignment: Text.AlignHCenter
        }

        // 新闻列表
        ScrollView {
            id: scrollView
            anchors.top: refreshButton.bottom
            anchors.bottom: weiyuContainer.top
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.topMargin: errorText.visible ? 70 : 20
            anchors.bottomMargin: 10
            anchors.leftMargin: 20
            anchors.rightMargin: 20
            
            clip: true

            ListView {
                id: newsListView
                width: parent.width
                spacing: 15
                
                model: newsManager.newsList
                
                delegate: Rectangle {
                    width: newsListView.width
                    height: Math.max(newsText.implicitHeight + 40, 60)
                    color: "#ffffff"
                    radius: 12
                    border.color: "#90caf9"
                    border.width: 2
                    
                    Text {
                        id: newsText
                        text: modelData
                        font.pixelSize: 26
                        font.family: "sans-serif"
                        color: "#212121"
                        anchors.fill: parent
                        anchors.margins: 20
                        wrapMode: Text.Wrap
                        verticalAlignment: Text.AlignVCenter
                        lineHeight: 1.4
                        
                        Component.onCompleted: {
                            console.log("新闻项:", modelData)
                        }
                    }
                }
            }
        }

        // 微语区域
        Rectangle {
            id: weiyuContainer
            width: parent.width - 40
            height: weiyuText.height + 40
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottomMargin: 20
            color: "#fff3e0"
            radius: 15
            border.color: "#ffb74d"
            border.width: 3
            visible: newsManager.weiyu !== ""
            
            Text {
                id: weiyuText
                text: newsManager.weiyu
                font.pixelSize: 24
                font.italic: true
                color: "#e65100"
                anchors.centerIn: parent
                width: parent.width - 40
                wrapMode: Text.WordWrap
                horizontalAlignment: Text.AlignHCenter
                lineHeight: 1.3
            }
        }

        // 空状态提示
        Text {
            visible: newsManager.newsList.length === 0 && !newsManager.loading && newsManager.errorMessage === ""
            text: "点击上方刷新按钮获取今日早报 📱"
            font.pixelSize: 32
            color: "#546e7a"
            anchors.centerIn: parent
        }
    }

    // 组件加载完成后自动获取新闻
    Component.onCompleted: {
        newsManager.fetchNews()
    }
}